﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace HYBatchPrinter.Commands
{
	public class PrinterFunctionManager
	{
		public IntPtr GetIntPtr()
		{
			return Process.GetCurrentProcess().MainWindowHandle;
		}

        public System.Windows.Forms.Form GetRevitForm()
		{
            return (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.GetIntPtr());
		}

		private static void setInsesionToCurrent(PrintManager prntMgr)
		{
			prntMgr.PrintSetup.CurrentPrintSetting = prntMgr.PrintSetup.InSession;
		}

		private void setCurrentPrintSettingByName(string sName, Document rvtDoc, PrintManager prntMgr)
		{
			Element element = PrinterFunctionManager.findPrintSettingByName(sName, rvtDoc);
			if (element != null)
			{
				prntMgr.PrintSetup.CurrentPrintSetting = (element as PrintSetting);
			}
		}

		private static Element findPrintSettingByName(string sName, Document rvtDoc)
		{
			Element result = null;
			if (!string.IsNullOrEmpty(sName))
			{
				foreach (ElementId elementId in rvtDoc.GetPrintSettingIds())
				{
					Element element = rvtDoc.GetElement(elementId);
					if (element.Name.Equals(sName))
					{
						result = element;
						break;
					}
				}
			}
			return result;
		}

		private static PaperSize GetRvtPaperSizeByName(PrintManager rvtPrintMgr, string sName)
		{
			PaperSize result = null;
			foreach (object obj in rvtPrintMgr.PaperSizes)
			{
				PaperSize paperSize = (PaperSize)obj;
				if (paperSize.Name.Equals(sName))
				{
					result = paperSize;
					break;
				}
			}
			return result;
		}

		private static bool createDefaultPrintSetting(string sName, UIApplication revUiApp)
		{
			bool result = false;
			Document document = revUiApp.ActiveUIDocument.Document;
			PrintManager printManager = revUiApp.ActiveUIDocument.Document.PrintManager;
			if (PrinterFunctionManager.findPrintSettingByName(sName, document) == null)
			{
				printManager.PrintSetup.CurrentPrintSetting.PrintParameters.ColorDepth = 0;
                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.ZoomType = (ZoomType)1;
				printManager.PrintSetup.CurrentPrintSetting.PrintParameters.Zoom = 100;
				printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement = 0;
                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = (Autodesk.Revit.DB.PageOrientationType)1;
				PaperSize rvtPaperSizeByName = PrinterFunctionManager.GetRvtPaperSizeByName(printManager, "A4");
				if (rvtPaperSizeByName != null)
				{
					printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize = rvtPaperSizeByName;
				}
				result = printManager.PrintSetup.SaveAs(sName);
			}
			return result;
		}

		public static bool Printting(ExternalCommandData commandData)
		{
			bool result = true;
			Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "print");
			transaction.Start();
			try
			{
				PrinterFunctionManager.createDefaultPrintSetting("HYPrintSetting", commandData.Application);
				transaction.Commit();
			}
			catch (Exception ex)
			{
				MessageBox.Show("默认打印设置创建失败:" + ex.Message, "盈建科提示");
				transaction.RollBack();
			}
			Transaction transaction2 = null;
			try
			{
				transaction2 = new Transaction(commandData.Application.ActiveUIDocument.Document, "ViewPrinter");
				transaction2.Start();
				PrintMgr printMgr = new PrintMgr(commandData);
				if (printMgr.InstalledPrinterNames == null)
				{
					return false;
				}
				if (new PrintMgrForm(printMgr, commandData.Application)
				{
					StartPosition = FormStartPosition.CenterScreen
				}.ShowDialog() != DialogResult.Cancel)
				{
					PrinterFunctionManager.setInsesionToCurrent(printMgr.getRvtDocPrinter());
				}
				transaction2.Commit();
			}
			catch (Exception ex2)
			{
				ex2.ToString();
				if (transaction2 != null)
				{
					transaction2.RollBack();
				}
				return false;
			}
			return result;
		}
	}
}
